package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PersonalController {
        @Autowired
        private IAccountService accountService;
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private IHostingService hostingService;


        @RequestMapping("personal")
        @IsLoginAnno
        public String personalList(Model model){
                //根据用户ID获取账户信息
                UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
                if(userInfo.canBorrow()){
                        model.addAttribute("hasRealAuth",true);
                }
                model.addAttribute("userInfo",userInfo);
                Account account = accountService.query(UserContext.getLoginInfo().getId());
                model.addAttribute("account",account);
                return "personal";
        }

        //跳转托管中心的方法
        @RequestMapping("hostingPersonal")
        public String hostingPersonal(){
                String url = hostingService.hostingPersonal(UserContext.getLoginInfo());
               return "redirect:"+url;
        }
        /**
         * 设置支付密码
         */
        @RequestMapping("hostingSetPayPassword")
        public String hostingSetPayPassword(){
                String url = hostingService.hostingSetPayPassword(UserContext.getLoginInfo());
                return "redirect:"+url;
        }
        @RequestMapping("callBackSetPayPassword")
        @ResponseBody
        public String callBackSetPayPassword(Long uid){
                userInfoService.updateHostingPayPassWord(uid);

                return "支付密码保存成功!";
        }
}
